package com.hzx.security.security;

import io.jsonwebtoken.CompressionCodec;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TokenManager {
    //有效时长
    private long expiraTime = 24*60;

    //编制密钥
    private String tokenSecretKey = "123456";


    //根据用户名生成token
    public String generateToken(String username){
        String token = Jwts.builder()
                .setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis()+expiraTime))
                .signWith(SignatureAlgorithm.ES512,tokenSecretKey)
                .compressWith(CompressionCodecs.GZIP).compact();

        return token;
    }

    //根据token获取用户信息
    public String getUserInfoByToken(String token){
        String userInfo = Jwts.parser().setSigningKey(tokenSecretKey)
                .parseClaimsJws(token).getBody()
                .getSubject();

        return userInfo;
    }

    public void removeToken(String token){

    }
}
